<?php 
include_once 'list.php';

class C_metas {
	
	private $tempLoaded = false;
	public 	$templateName;
	private $secModLoaded = false;
	private	$securityModule;
	public 	$moduleList;
	public  $posList;
	public 	$modcount;
	public  $isRegistered = false;
	
	function addPlugin($plugName) {
		if (preg_match("/^sec_/", $plugName)) {
			$this->securityModule = $plugName;
			$this->secModLoaded = true;
		}
	}
	
	function C_metas($tempName, $plugins) {
		if (isset($tempName)) {
			$this->templateName = $tempName;
			$this->tempLoaded = true;
		}
		reset($plugins);
		while (list($key,$plug) = each($plugins)) {
			$this->addPlugin($plug);
		}
	}
	
	function begin() {
		$this->moduleList = new LinkList();
		$this->posList = new LinkList();
		$this->modCount = 0;
	}
	function begint($tempName) {
		$this->begin();
		$this->templateName = $tempName;
		$this->tempLoaded = true;
	}
	
	function end() {
		if ($this->tempLoaded == false || $this->secModLoaded == false)
			die("At least a security module and a graphical template must be loaded!");
		
		include 'base_plugin.php';
		include 'base_module.php';
		include_once 'swissknife.php';
		$tools = new C_tools($this);
		
		$tempName = 'metas/templates/'. $this->templateName. '/index.php';
		$secName = 'metas/plugins/'. $this->securityModule. '/plug.php';
		
		include $secName;
		$plug->setToolkit($tools);
		
		if ($tools->getGet('logout')==1) {
			$plug->secPerformLogout();
		}
		if ($tools->getGet('showlogin')==1) {
			$plug->secPerformLogin();
		}
		$this->isRegistered = $plug->secCheckAuth();
		$tools->setSecPlugin($plug);
		
		if (!$this->isRegistered && $tools->getGet('showlogin')==1) {
			$tempName = 'metas/templates/'. $this->templateName. '/login.php';
		
		} else if ($tools->getGet('register')==1) {
			$tempName = 'metas/templates/'. $this->templateName. '/registercomplete.php';
		}

		if ($tools->getGet('mod')==1) {
			$mName = $tools->getGet('name');
			if (strlen($mName)>0) {
				$tools->loadModule($mName);
				return;
			} else die('invalid request');
		}
		include $tempName;
		if ($tools->getGet('register')==1) {
			if ($plug->secPerformRegister()) {
				echo 'Registrazione completata';
			}
		}
	}
	
	function loadModule($modName, $position) {
		$this->moduleList->insertLast($modName);
		$this->posList->insertLast($position);
		$this->modCount++;
	}
	
}

?>